﻿var common={ // 공통함수
	serverError : "서버에 이상이 있습니다.",
	pageEndNumBase : 10,
	alert: function(msg, fn) { // jQuertUI alert

		$("#dialog").find("p").text(msg).end().dialog({
			resizable: false,
			modal: true,
			width: 350,
			open: function() {
				if($.browser.msie && $.browser.version=="6.0") $("select").addClass("hidden");
			},
			close: function() {
				if($.browser.msie && $.browser.version=="6.0") $("select").removeClass("hidden");
				if(typeof(fn) == "function") fn();
			},
			buttons: {
				'확인': function() {
					$(this).dialog('close');
				}
			}
		});
	},

	confirm: function(msg, obj, fn, modal) { // jQuertUI confirm
		if(typeof(obj) != "object") return; // 컨펌창에서 버튼이 없으면 안된다.
		if(modal==undefined) modal=false; // 기본값 false

		$("#dialog").find("p").text(msg).end().dialog({
			modal: modal,
			width: 350,
			open: function() {
				if($.browser.msie && $.browser.version=="6.0") $("select").addClass("hidden");
			},
			close: function() {
				if($.browser.msie && $.browser.version=="6.0") $("select").removeClass("hidden");
				if(typeof(fn) == "function") fn();
			},
			buttons: obj
		});
	},

	dialogPopup : function($dialogForm, btnSetting, width, height, resizable){
		if(!$dialogForm || !btnSetting){
			commmon.alert("잘못된 접근입니다.");
		}
		if(!width || width == 0) width = 400;
		if(!height || height == 0) height = 520;
		$dialogForm.dialog({
			resizable: resizable,
			autoOpen: false,
			height: height,
			width: width,
			modal: true,
			buttons: btnSetting,
			close: function() {
				$(this).find(":input").val( "" ).prop("readonly", false).removeClass( "ui-state-error" );
			}
		});
	},

	isEmpty : function(data) {
		if(!data || data  =='undefined'){
			data = "";
		}
		return data;
	},
	isEmptyDate : function(date){
		if(!date || date  =='undefined'){
			date = "";
		}else{
			var dateVal = new Date(date);
			var month = dateVal.getMonth()+1;
			var day = dateVal.getDate();
			if(month < 10) month = "0" + month;
			if(day < 10) day = "0" + day;
			date = dateVal.getFullYear() + "-" + month + "-" + day;
		}
		return date;
	},
	/*
	 * 데이터 삽입 후 페이징 reset
	 */
	insertToPaging : function($pagingForm, total, pageEndNumBase){
		var pageTotal = Math.ceil(total/pageEndNumBase);

		var pageEnd = 6;
    	if(pageTotal < 10 || pageEnd > pageTotal){
    		pageEnd = pageTotal;
    	}else if(pageTotal > 10 && pageEnd < 10){
    		pageEnd = 10;
    	}
		return common.paging($pagingForm, pageTotal, 1, pageEnd, 1);
	},

	paging : function($pagingForm, pageTotal, pageStart, pageEnd, pageNum){
		pageNum = parseInt(pageNum, 10);
		pageTotal = parseInt(pageTotal, 10);
		$pagingForm.empty();
		if(pageStart > 1 && pageNum > 5){
			$pagingForm.append(
					$("<a/>").attr({ "pageNum": pageNum-1, "title": "페이지 뒤로 이동" }).addClass("direction").text("‹").append(
							$("<span/>").text("Prev")
					));
		}

		if(!pageNum || pageNum == 0){
			$pagingForm.append($("<strong>").attr("title", "현재 1 페이지").text("1"));
		}else{
			for(pageStart; pageStart <= pageEnd; pageStart++){
				if(pageStart > pageTotal){
					break;
				}else if(pageNum==pageStart){
					$pagingForm.append($("<strong>").attr("title", "현재 " + pageStart + " 페이지").text( pageStart ));
				}else{
					$pagingForm.append($("<a/>").attr({ "pageNum": pageStart, "title": (pageStart + " 페이지로 이동")}).text(pageStart));
				}
			}
		}

		if(pageTotal > 10 && (pageNum <= 5 || pageTotal > (pageNum + 5))){
			$pagingForm.append(
					$("<a/>").attr({ "pageNum": pageNum+1, "title": "페이지 앞으로 이동" }).addClass("direction").text("›").append(
							$("<span/>").text("Next")
					));
		}

		$pagingForm.children().css("margin-right", "1px");
	},

	search : function(){
		var $headSearch = $("#headSearch");
		var commonQuery = $headSearch.find("[name=commonQuery]").val();
		if(commonQuery) {
			$headSearch.find("[name=logMsg]").val("검색조건 -> 검색어 : "+commonQuery);
			return true;
		}
		return false;
	}
};


var listAllChkbox = {
	chkAll : function(){
		var $base = $(this).closest("table");
		var $temp = $base.find("tbody");

		if($temp.find(":checkbox").length==$temp.find(":checkbox:checked").length){
			$base.find(":checkbox").prop("checked", false).end().find(".choice").removeClass("choice");
		}else
			$base.find(":checkbox").prop("checked", true).end().find("tbody").find("tr").addClass("choice");
	}
};

$(function(){
	$.datepicker.regional['ko'] = {
			  closeText: '닫기',
			  prevText: '이전달',
			  nextText: '다음달',
			  currentText: '오늘',
			  monthNames: ['1월','2월','3월','4월','5월','6월',
			  '7월','8월','9월','10월','11월','12월'],
			  monthNamesShort: ['1월','2월','3월','4월','5월','6월',
			  '7월','8월','9월','10월','11월','12월'],
			  dayNames: ['일','월','화','수','목','금','토'],
			  dayNamesShort: ['일','월','화','수','목','금','토'],
			  dayNamesMin: ['일','월','화','수','목','금','토'],
			  weekHeader: 'Wk',
			  dateFormat: 'yy-mm-dd',
			  firstDay: 0,
			  isRTL: false,
			  duration:200,
			  showAnim:'show',
			  showMonthAfterYear: true,
		      showOn: "button",
		      buttonImage: contextPath+"/res/css/images/calendar.gif",
		      buttonImageOnly: true,
		      changeMonth: true,
		      changeYear: true};
			      $.datepicker.setDefaults($.datepicker.regional['ko']);

	$(".date").datepicker().mask("9999-99-99");

	$(".zipcode").mask("999-999");
	$(".tel").mask("999-9999-9999");
	$(".ssn").mask("999-99-99999");

	$(document).bind("ajaxStart", function(){
		$("#loading").show();
	}).bind("ajaxStop", function(){
		$("#loading").hide();
	});

	$(".excelDown").click(function(){
		var href = $(this).attr("href");

		$(this).closest(".searchForm").clone().attr("action", href).submit();
	});


});

